package com.cat.dataStructure02;

import java.util.HashMap;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/k-diff-pairs-in-an-array/
 * @create 2025/10/21 19:49
 * @since JDK17
 */

public class Solution38 {
    public int findPairs(int[] nums, int k) {
        int n = nums.length, ans = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int x : nums) {
            map.merge(x, 1, Integer::sum);
        }
        for (int x : map.keySet()) {
            if (k == 0) {
                ans += map.getOrDefault(x, 0) > 1 ? 1 : 0;
            } else {
                ans += map.containsKey(x + k) ? 1 : 0;
            }
        }

        return ans;
    }
}
